package com.systemservice.controller;

import annotation.DisableBaseResponse;
import com.systemservice.service.RoleService;
import generator.domain.Role;
import generator.support.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/role")
@CrossOrigin
@Slf4j
public class roleController {

    @Autowired
    RoleService roleService;

    /**
     * 获取所有身份信息
     */
    @GetMapping("/roles")
    public List<Role> getRoles(){
        log.info("RoleController -> getRoles");
        return roleService.list();
    }

    /**
     * 删除身份信息
     */
    @GetMapping("/delete/{id}")
    @DisableBaseResponse
    public ResponseResult<Object> deleteRole(@PathVariable String id){
        log.info("RoleController -> deleteRole");
        try{
            int a = Integer.parseInt(id);
        } catch (NumberFormatException e){
            return new ResponseResult<>(400, "请检查该身份是否存在");
        }
        return roleService.deleteRoleById(Integer.parseInt(id));
    }

    /**
     * 添加身份信息
     */
    @PostMapping("/addRole")
    @DisableBaseResponse
    public ResponseResult<Object> addRole(@RequestBody Role role){
        log.info("RoleController -> addRole");
        return roleService.addRole(role);
    }

    /**
     * 修改角色信息
     */
    @PostMapping("/edit")
    @DisableBaseResponse
    public ResponseResult<Object> editRole(@RequestBody Role role){
        log.info("RoleController -> editRole");
        return roleService.editRole(role);
    }
}
